'use strict';

import { Controller } from 'egg';


export default class UserController extends Controller {
  /**
   * 登录
   */
  async login() {
    const { w_validate_name, w_validate_pass, w_validate_code } = this.ctx
    this.ctx.validate({
      name: w_validate_name,
      pass: w_validate_pass,
      code: w_validate_code,
      uuid: {
        type: 'uuid'
      }
    });
    const data = await this.ctx.service.user.login(this.ctx.w_body());
    this.ctx.rotateCsrfSecret();
    this.ctx.sendSuccess(data);
  }
  /**
   * 获取用户数据
   */
  async sessionLogin() {
    const data = await this.ctx.service.user.sessionLogin();
    this.ctx.sendSuccess(data);
  }

  /**
   * 获取验证码 Verification code
   */
  async verificationCode() {
    const data = await this.ctx.service.user.verificationCode();
    this.ctx.sendSuccess(data);
  }
  
  /**
   * 退出登录
   */
  async logut() {
    this.ctx.service.user.logut();
    this.ctx.sendSuccess('OK');
  }

  /**
 * 创建账号
 */
  async create() {
    const { w_validate_name, w_validate_pass, w_validate_code } = this.ctx
    this.ctx.validate({
      name: w_validate_name,
      pass: w_validate_pass,
      code: w_validate_code,
      type: {
        type: 'enum',
        values: [ 'SUPER_ADMIN', 'ADMIN' ],
      },
      uuid: {
        type: 'uuid',
      },
    });
    await this.ctx.service.user.createAccount(this.ctx.w_body('name pass code type uuid'));
    this.ctx.sendSuccess('OK');
};

/**
 * 获取列表数据
 */
async index() {
  this.ctx.w_validate_page();
  this.ctx.validate({
    type: {
      type: 'enum',
      values: [ 'SUPER_ADMIN', 'ADMIN', 'ALL' ],
    },
  }, this.ctx.query);
  const data = await Promise.all([ this.ctx.service.user.list(), this.ctx.service.user.count() ]);
  this.ctx.sendList(data);
};

  /**
   * 修改密码
   */
  async updatePassword() {
    const { w_validate_pass, w_validate_code } = this.ctx
    this.ctx.validate({
      newPass: w_validate_pass,
      oldPass: w_validate_pass,
      code: w_validate_code,
      uuid: { type: 'uuid' }
    });
    await this.ctx.service.user.updatePassword(this.ctx.w_body('newPass oldPass code uuid'));
    this.ctx.rotateCsrfSecret();
    this.ctx.sendSuccess('OK');
  }

  /**
   * 修改密码(修改其他人的密码)
   */
  async changeOtherPassword() {
    const { w_validate_pass, w_validate_code } = this.ctx
    this.ctx.validate({
      newPass: w_validate_pass,
      code: w_validate_code,
      id: {
        type: 'mongo-id',
      },
      uuid: {
        type: 'uuid',
      },
    });
    await this.ctx.service.user.changeOtherPassword(this.ctx.w_body('newPass code id uuid'));
    this.ctx.sendSuccess('OK');
  }

  /**
   * 修改用户状态
   */
  async userEnabled() {
    this.ctx.validate({
      enabled: {
        type: 'boolean',
      },
      id: {
        type: 'mongo-id',
      },
    });
    await this.ctx.service.user.userEnabled(this.ctx.w_body())
    this.ctx.sendSuccess('OK');
  }

  /**
   * 删除用户
   */
  async destroy() {
    this.ctx.validate({
      id: {
        type: 'mongo-id',
      },
    }, this.ctx.query);
    const { id } = this.ctx.query
    await this.ctx.service.user.removeAccount(id)
    this.ctx.sendSuccess('OK');
  }

  /**
   * 添加项目
   */
  async addProject() {
    this.ctx.w_validate_param_id()
    this.ctx.validate({
      projectId: {
        type: 'mongo-id',
      },
    });
    await this.ctx.service.user.addProject({
      userId: this.ctx.params.id,
      projectId: this.ctx.w_body().projectId
    })
    this.ctx.sendSuccess('OK');
  }

  /**
   * 删除项目
   */
  async removeProject() {
    this.ctx.w_validate_param_id()
    this.ctx.validate({
      projectId: {
        type: 'mongo-id',
      },
    }, this.ctx.query);
    await this.ctx.service.user.removeProject({
      userId: this.ctx.params.id,
      projectId: this.ctx.query.projectId
    })
    this.ctx.sendSuccess('OK');
  }

  /**
   * 获取当前账号下面的项目列表
   */
  async projectList() {
    const list = await this.ctx.service.user.projectList()
    this.ctx.sendSuccess(list);
  }

  /**
   * 重置密码
   */
  async resetPass() {
    if (this.ctx.app.config.env === 'prod') {
      throw '不要想太多，这个接口只能在本地跑的起来'
    }
    const { w_validate_name } = this.ctx
    this.ctx.validate({
      name: w_validate_name,
    });
    await this.ctx.service.user.resetPass(this.ctx.w_body().name)
    this.ctx.sendSuccess('OK');
  }
}